<template>
  <div style="height: 100vh; background: #f5f5f5">
    <!-- <div class="nav_bar">
      <van-nav-bar
        left-text="返回"
        title="添加盘点异常数据"
        left-arrow
        fixed
        placeholder
        @click-left="goBack"
      />
    </div> -->
    <!-- <div style="height: 0.5rem; background: red"></div> -->
    <!-- <div class="otherMargin"></div> -->
    <div
      class="search-content wrap-apply"
      :style="{top: GLOBAL.currentEnvironment ? '0!important' : '0.52rem',height: GLOBAL.currentEnvironment ? '100vh' : 'calc(100vh - 1.12rem)',paddingBottom: GLOBAL.currentEnvironment ? '0.66rem' : ''}"
    >
      <van-cell-group>
        <p class="label required FormTit"><span></span>盘点合格品（片）</p>
        <van-field
          @input="changeNum(changeNumDataCopy.item)"
          v-model="changeNumDataCopy.item.inventoryGoodsQualifiedNum"
          placeholder="请输入"
        >
          <template slot="right-icon">
            <span>
              <van-dropdown-menu active-color="#1890ff">
                <van-dropdown-item
                  v-model="changeNumDataCopy.item.selectUnitQ"
                  :options="unitOpyions(changeNumDataCopy.item.unitList)"
                  @change="
                    (v) =>
                      changeNumUnit(
                        v,
                        changeNumDataCopy.item,
                        'selectUnitQ',
                        'rateQ',
                        'inventoryGoodsQualifiedNum'
                      )
                  "
                />
              </van-dropdown-menu>
            </span>
          </template>
        </van-field>
        <div class="bottomLine"></div>
      </van-cell-group>
      <van-cell-group>
        <p class="label required FormTit"><span></span>盘点报废品（片）</p>
        <van-field
          @input="changeNum2(changeNumDataCopy.item)"
          v-model="changeNumDataCopy.item.inventoryGoodsScrapNum"
          placeholder="请输入"
        >
          <template slot="right-icon">
            <span>
              <van-dropdown-menu active-color="#1890ff">
                <van-dropdown-item
                  v-model="changeNumDataCopy.item.selectUnitS"
                  :options="unitOpyions(changeNumDataCopy.item.unitList)"
                  @change="
                    (v) =>
                      changeNumUnit(
                        v,
                        changeNumDataCopy.item,
                        'selectUnitS',
                        'rateS',
                        'inventoryGoodsScrapNum'
                      )
                  "
                />
              </van-dropdown-menu>
            </span>
          </template>
        </van-field>
        <div class="bottomLine"></div>
      </van-cell-group>
      <div class="search-bottom">
        <span class="btnconfirm" @click="btnConfirm">确定</span>
      </div>
    </div>
  </div>
</template>
<script>
import { clearNoNum, accDivMin, accMul, accMulMin } from "@/utils/util";

export default {
  props: {
    changeNumData: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  name: "changeNum", //添加盘点异常数据弹窗
  components: {},

  data() {
    return {
      //领用时间
      buildForm: {
        inventoryGoodsQualifiedNum: "", //盘点合格品
        inventoryGoodsScrapNum: "", //盘点报废品
      },
      changeNumDataCopy: Object.assign({}, this.changeNumData),
    };
  },
  computed: {},
  mounted() {},
  created() {
    console.log(this.changeNumDataCopy);
  },
  methods: {
    changeNumUnit(v, data, key, rate, inputKey) {
      let unitList = data.unitList;
      let unit = unitList.find((u) => u.id == v);
      let old_num = this.changeNumDataCopy.item[inputKey] ? accMul(this.changeNumDataCopy.item[inputKey], this.changeNumDataCopy.item[`${rate}Old`]) : '' /* 当前数值乘以单位换算比例，得到对应的基本单位数量 */
      this.changeNumDataCopy.item[key] = v;
      this.changeNumDataCopy.item[rate] = unit.conversionRate;
      this.changeNumDataCopy.item[`${rate}Old`] = unit.conversionRate;
      this.changeNumDataCopy.item[inputKey] = this.changeNumDataCopy.item[inputKey] ? accDivMin(old_num, unit.conversionRate) : '0' /* 基本单位数量除以辅助单位比例，得到当前数值对应辅助单位数量 */
      this.$forceUpdate();
    },
    unitOpyions(v) {
      let list = v;
      let val = list.map((item) => {
        item.value = item.id;
        item.text = item.unit;
        return item;
      });
      return val;
    },
    //   input事件
    changeNum(item) {
      item.inventoryGoodsQualifiedNum = clearNoNum(
        item.inventoryGoodsQualifiedNum
      );
    },
    //   input事件
    changeNum2(item) {
      item.inventoryGoodsScrapNum = clearNoNum(item.inventoryGoodsScrapNum);
    },
    goBack() {
      this.$emit("goBack", this.changeNumDataCopy.item);
    },
    // 重置
    btnCancel() {},
    // 表单提交
    btnConfirm() {
      let obj = this.changeNumDataCopy.item;
      let qualifiedUnit = [];
      let scrapUnit = [];
      let numQ = accMul(
        obj.inventoryGoodsQualifiedNum,
        obj.rateQ
      );
      let numS = accMul(
        obj.inventoryGoodsScrapNum,
        obj.rateS
      );
      // if (qualifiedUnit.unitType != "basic_unit") {
      //   this.changeNumDataCopy.item.inventoryGoodsQualifiedNumTextAssistUnit = `${obj.inventoryGoodsQualifiedNum}${qualifiedUnit.unit}`;
      // } else {
      //   this.changeNumDataCopy.item.inventoryGoodsQualifiedNumTextAssistUnit =
      //     "";
      // }
      // if (scrapUnit.unitType != "basic_unit") {
      //   this.changeNumDataCopy.item.inventoryGoodsScrapNumTextAssistUnit = `${obj.inventoryGoodsScrapNum}${scrapUnit.unit}`;
      // } else {
      //   this.changeNumDataCopy.item.inventoryGoodsScrapNumTextAssistUnit = "";
      // }
      for (let u = 0; u < obj.unitList.length; u++) {
        const unit = obj.unitList[u];
        if (unit.unitType != "basic_unit") {
          let textQ = accDivMin(numQ, unit.conversionRate)
          let textS = accDivMin(numS, unit.conversionRate)
          qualifiedUnit.push(`${textQ}${unit.unit}`)
          scrapUnit.push(`${textS}${unit.unit}`)
        }
      }
      console.log("qualifiedUnit.join('/'):",qualifiedUnit.join('/'));
      console.log("scrapUnit.join('/'):",scrapUnit.join('/'));
      this.changeNumDataCopy.item.inventoryGoodsQualifiedNumTextAssistUnit = qualifiedUnit.join('/');
      this.changeNumDataCopy.item.inventoryGoodsScrapNumTextAssistUnit = scrapUnit.join('/');
      this.changeNumDataCopy.item.inventoryGoodsQualifiedNum = accMulMin(
        obj.inventoryGoodsQualifiedNum,
        obj.rateQ
      );
      this.changeNumDataCopy.item.inventoryGoodsScrapNum = accMulMin(
        obj.inventoryGoodsScrapNum,
        obj.rateS
      );
      this.goBack();
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/theme.scss";
@import "@/views/workManagement/materialRequisition/materialDetail.scss";
@import "@/views/workManagement/materialRequisition/materialForm.scss";
/deep/ .van-nav-bar {
  background-color: #ffffff;
  color: #191f25;
  z-index: 999;
}

/deep/ .van-icon {
  color: #191f25;
}

/deep/ .van-nav-bar__text {
  color: #191f25;
}

/deep/ .van-nav-bar__title {
  color: #191f25;
}
/deep/ .van-popover__content {
  background: red;
  box-shadow: 0rem 0rem 0rem 0rem rgba(0, 0, 0, 0.3);
  border-radius: 2rem;
}
.search-content {
  height: 100vh;
  top: 58px;
}
/deep/.van-dropdown-menu__bar {
  height: auto;
  box-shadow: none;
}
</style>
